package com.iyue.util.file;

import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.HttpVersion;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpClientParams;

import com.iyue.Config;
import com.iyue.SpringHelper;
import com.iyue.common.base.AbstractText;
import com.iyue.servlet.taglibs.Functions;

/**
 * 
 * 
 * @author 孙晓晔
 * @version $Id: FileUtil.java,v 1.3 2010/09/02 22:27:40 tangliang Exp $
 */
public final class FileUtil {
	/**
	 * 根据url地址获取文件类型
	 * 
	 * @param util
	 * @param s
	 * @return
	 */
	public static Integer getMime(String s) {
		s = s.toLowerCase();

		if (s == null || s.equals("")) {
			return AbstractText.RES_NONE;
		}

		if (s.matches("[\\w|\\W]+(\\.)+(wav|amr|mp3)$")) {
			return AbstractText.RES_SOUND;
		}

		if (s.matches("[\\w|\\W]+(\\.)+(3gp|mp4)$")) {
			return AbstractText.RES_VIDEO;
		}

		if (s.matches("[\\w|\\W]+(\\.)+(jpg|jpeg|png|gif)$")) {
			return AbstractText.RES_IMAGE;
		}

		return AbstractText.RES_NONE;
	}

	public static String url(String url) {
		int pos = url.lastIndexOf("/");
		String prefix = url.substring(0, pos + 1);
		url = prefix + Functions.encode(filename(url));

		return url;
	}

	/**
	 * 计算文件名
	 * 
	 * @param url
	 * @return
	 */
	public static String filename(String url) {
		String ret;
		int pos = url.lastIndexOf("/");
		if (pos > 0) {
			ret = url.substring(pos + 1);
		} else {
			ret = url;
		}

		return ret;
	}

	public static Map<String, String> upload(HttpServletRequest request,
			String type, String username) {
		FileUpload fu = (FileUpload) SpringHelper.getBean("fileupload");

		return fu.upload(request, type, username);
	}

	public static String upload(String url, String username)
			throws IOException {
		if (url == null || !url.startsWith("http://")) {
			return null;
		}

		HttpClient http = new HttpClient();
		HttpClientParams params = http.getParams();
		params.setParameter("http.protocol.version", HttpVersion.HTTP_1_1);
		params.setParameter("http.socket.timeout", new Integer(3000));
		params.setParameter("http.protocol.content-charset", "UTF-8");

		HttpMethod method = new GetMethod(url);
		method.setRequestHeader("User-Agent", "Nokia3100");

		int sc = http.executeMethod(method);
		if (sc != HttpStatus.SC_OK) {
			return null;
		}

		InputStream in = method.getResponseBodyAsStream();
		FileUpload fu = (FileUpload) SpringHelper.getBean("fileupload");

		Config config = (Config) SpringHelper.getBean("test");
		fu.setUrl(config.getFile() + "/j2up.do");

		return fu.upload(in, FileUtil.filename(url), username);
	}

	public static byte[] upload(String url) throws IOException {
		if (url == null || !url.startsWith("http://")) {
			return null;
		}

		HttpClient http = new HttpClient();
		HttpClientParams params = http.getParams();
		params.setParameter("http.protocol.version", HttpVersion.HTTP_1_1);
		params.setParameter("http.socket.timeout", new Integer(3000));
		params.setParameter("http.protocol.content-charset", "UTF-8");

		HttpMethod method = new GetMethod(url);
		method.setRequestHeader("User-Agent", "Nokia3100");

		int sc = http.executeMethod(method);
		if (sc != HttpStatus.SC_OK) {
			return null;
		}

		byte[] ret = method.getResponseBody();

		return ret;
	}
}
